﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OCDStudio.SqlAzureBackup.Client.Code;

namespace OCDStudio.SqlAzureBackup.Client
{
    public enum SettingPages
    {
        GENERAL,
        CONNECTION,
        BACKUP,
        PROFILES
    }

    public partial class SettingsForm : Form
    {

        public delegate void SimpleNotifier();

        public event SimpleNotifier OnGeneralSaved;
        public event SimpleNotifier OnConnectionSaved;
        public event SimpleNotifier OnBackupSaved;
        public event SimpleNotifier OnProfilesSaved;
        
        public SettingsForm(SettingPages tab=SettingPages.GENERAL)
        {
            InitializeComponent();
            tabs.SelectedIndex = (int)tab;
        }

        private void cmdAccept_Click(object sender, EventArgs e)
        {
            SaveGeneral();
            SaveConnection();
            SaveBackup();
            SaveProfiles();
            Dispose();
        }

        private void SaveProfiles()
        {
            ProfileManager.StringProfiles = txtConnectionProfiles.Text;
            if (OnProfilesSaved != null) OnProfilesSaved();
        }

        private void SaveBackup()
        {

            if (OnBackupSaved != null) OnBackupSaved();
        }

        private void SaveConnection()
        {

            if (OnConnectionSaved != null) OnConnectionSaved();
        }

        private void SaveGeneral()
        {

            if (OnGeneralSaved != null) OnGeneralSaved();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            LoadGeneral();
            LoadConnection();
            LoadBackup();            
            LoadProfiles();
        }

        private void LoadProfiles()
        {
            txtConnectionProfiles.Text = ProfileManager.StringProfiles;
            lblHowManyProfiles.Text = ProfileManager.Profiles.Count.ToString();
        }

        private void LoadConnection()
        {
        }

        private void LoadGeneral()
        {
        }

        private void LoadBackup()
        {
        }

        private void cmdCopyConnectionProfiles_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetText(txtConnectionProfiles.Text, TextDataFormat.UnicodeText);            
            }
            catch {}
        }

        private void cmdPasteConnectionProfiles_Click(object sender, EventArgs e)
        {
            txtConnectionProfiles.Multiline = true;
            txtConnectionProfiles.Text = Clipboard.GetText();
            txtConnectionProfiles.Multiline = false;
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        
    }
}
